<?php

namespace app\model\freight;

use think\Model;

/**
 * t_freight
 * @property integer $id ID(主键)
 * @property string $name 运费模版名称
 * @property string $method 计费方式:10=按件数,20=按重量
 * @property integer $weigh 权重
 * @property integer $create_time create_time
 * @property integer $update_time 更新时间
 */
class Freight extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_freight';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';
    protected $append = [
        "method_text"
    ];
    const METHOD_NUM = 10;
    const METHOD_WEIGHT = 20;
    const METHOD_VOLUME = 30;

    const METHOD_TEXT_MAP = [
        self::METHOD_NUM => "按数量",
        self::METHOD_WEIGHT => "按重量",
        self::METHOD_VOLUME => "按体积",
    ];

    protected function getMethodTextAttr($value, $data)
    {
        if (isset($data['method'])) {
            return self::METHOD_TEXT_MAP[$data['method']] ?? "";
        }
        return "";
    }


    protected function rules()
    {
        return $this->hasMany(FreightRule::class, 'freight_id', 'id');
    }
}
